//
//  AlbumViewController.m
//  Sight
//
//  Created by chenheren on 16/8/9.
//  Copyright © 2016年 chen. All rights reserved.
//

#import "AlbumViewController.h"
#import "ImageViewController.h"
#import <Photos/Photos.h>

@interface AlbumViewController ()<UIPageViewControllerDataSource,UIPageViewControllerDelegate>

@property (strong,nonatomic) UIPageViewController *pageViewController;
@property (strong,nonnull) PHFetchResult *assetsFetchResults;

@property (copy,nonatomic) UIImage *previousImage;
@property (copy,nonatomic) UIImage *nextImage;

@end


@implementation AlbumViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    PHFetchOptions *fetchoptions = [[PHFetchOptions alloc] init];
    fetchoptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
    self.assetsFetchResults = [PHAsset fetchAssetsWithOptions:fetchoptions];
    
    NSLog(@"%ld",self.assetsFetchResults.count);
    

    
    //实例化UIPageViewController
    NSDictionary *options =[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:10]forKey: UIPageViewControllerOptionSpineLocationKey];
    
    self.pageViewController = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:options];
    
    self.pageViewController.dataSource =self;
    self.pageViewController.delegate = self;
    self.pageViewController.view.frame = self.view.bounds;
    
    ImageViewController *imageViewController = [self viewControllerAtIndex:0];
    imageViewController.imageView.image = self.currentImage;
    NSArray *viewControllers = @[imageViewController];
    
    [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
    [self addChildViewController:self.pageViewController];
    [self.view addSubview:self.pageViewController.view];
    
    //设置返回按钮,最后添加，防止被掩盖
    UIButton *gobackBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [gobackBtn setTitle:@"<返回" forState:UIControlStateNormal];
    gobackBtn.frame = CGRectMake(0, 20, 50, 20);
    [gobackBtn addTarget:self action:@selector(goback) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:gobackBtn];
    
    PHImageManager *imageManager = [PHImageManager defaultManager];
    [imageManager requestImageForAsset:self.assetsFetchResults[1]
                            targetSize:self.pageViewController.view.frame.size
                           contentMode:PHImageContentModeAspectFill
                               options:nil
                         resultHandler:^(UIImage *result, NSDictionary *info) {
                             self.nextImage = [result copy];
                             NSLog(@"nextImage is OK");
                         }];
}

-(void)goback{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - Latest photo

-(ImageViewController *)viewControllerAtIndex:(NSInteger)index{
    if (self.assetsFetchResults.count == 0 || index >=self.assetsFetchResults.count) {
        return nil;
    }
    
    
    ImageViewController *imageViewController = [[ImageViewController alloc]init];
    imageViewController.imageView.image = [UIImage imageNamed:@"demo.jpg"];
    imageViewController.index = index;
    return imageViewController;
    
    
}

-(NSInteger) indexOfViewController:(ImageViewController *)imageViewController{
    return imageViewController.index;
}

#pragma mark - UIPageViewControllerDateSource

-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{
    NSInteger index = [self indexOfViewController:(ImageViewController *)viewController];
    
    if (index ==0 ||index == NSNotFound) {
        return nil;
    }
    
    index --;
    
    ImageViewController *imageViewController =[self viewControllerAtIndex:index];
    imageViewController.imageView.image = [self.previousImage copy];
    
    UIImage *temp= [self.currentImage copy];
    self.currentImage = self.previousImage ;
    self.nextImage = temp;
    PHImageManager *imageManager = [PHImageManager defaultManager];
    [imageManager requestImageForAsset:self.assetsFetchResults[index]
                            targetSize:self.pageViewController.view.frame.size
                           contentMode:PHImageContentModeAspectFill
                               options:nil
                         resultHandler:^(UIImage *result, NSDictionary *info) {
                             self.previousImage = result;
                         }];
    
    return viewController;
}

-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{
    NSInteger index = [self indexOfViewController:(ImageViewController *)viewController];
    
    if (index == NSNotFound) {
        return nil;
    }
    
    index ++;
    
    if (index == self.assetsFetchResults.count) {
        return nil;
    }
    
    ImageViewController *imageViewController =[self viewControllerAtIndex:index];
    imageViewController.imageView.image = [self.nextImage copy];
    
    UIImage *temp= [self.currentImage copy];
    self.currentImage = self.nextImage ;
    self.previousImage = temp;
    PHImageManager *imageManager = [PHImageManager defaultManager];
    [imageManager requestImageForAsset:self.assetsFetchResults[index]
                            targetSize:self.pageViewController.view.frame.size
                           contentMode:PHImageContentModeAspectFill
                               options:nil
                         resultHandler:^(UIImage *result, NSDictionary *info) {
                             self.nextImage = result;
                         }];
    
    
    return viewController;
}



@end
